﻿package com.cw.war.ui 
{
	import flash.events.Event;
	import flash.utils.Dictionary;
	import com.cw.war.plaf.PlafManager;

	/**
	 * 重定位器
	 * @author DanielLu
	 */
	public class Resizer 
	{	
		private static var map : Dictionary = new Dictionary( );

		/**
		 * 添加重定位器
		 * @param resizer		重定位器
		 */
		public static function addResizer (resizer : IResizer) : void
		{
			if(map[resizer] == null)
			{
				var listener : Object = new Object( );
				listener.onResize = function(e : Event):void
				{
					if(resizer.getResizeDisplayObject( ).stage)
					{
						resizer.doResize( PlafManager.stage );
					}
				};
				resizer.getResizeDisplayObject( ).addEventListener( Event.ADDED_TO_STAGE , listener.onResize );
				PlafManager.stage.addEventListener( Event.RESIZE , listener.onResize );
				map[resizer] = listener;
			}
		}

		/**
		 * 删除重定位器
		 * @param resizer		重定位器
		 */
		public static function removeResizer (resizer : IResizer) : void
		{
			if(map[resizer] != null)
			{
				resizer.getResizeDisplayObject( ).removeEventListener( Event.ADDED_TO_STAGE , map[resizer].onResize );
				PlafManager.stage.removeEventListener( Event.RESIZE , map[resizer].onResize );
				delete map[resizer];
			}
		}
	}
}
